#ifndef Thumbnail_H
#define Thumbnail_H

#include <QtGui>
#include <QWidget>
#include <QPainter>
#include <QtEvents>
#include <QTimer>
#include <QPropertyAnimation>
#include <QProcess>
#include <QMenu>
#include <QTreeWidgetItem>
#include <defines.h>
#include "stdlib.h"
#include "editdialog.h"
#include "organizerwidget.h"
#include "settingsdialog.h"
#include "dummydata.h"

class Thumbnail: public QWidget
{
    Q_OBJECT
    Q_PROPERTY(QPoint rectOff READ getRectOff WRITE setRectOff RESET resetRectOff())
    Q_PROPERTY(QPoint tagMarq READ getTagMarq WRITE setTagMarq RESET resetTagMarq)

public:

    Thumbnail(item& _data, QWidget *parent = 0);
    QSize sizeHint() const;
    void setImage(const QString &fileName);
    void setData(const item& _data,QTreeWidgetItem* ref);
    void enterEvent(QEvent *e);
    void leaveEvent(QEvent *e);
    void mouseDoubleClickEvent(QMouseEvent *);

    item& getData(){return data;}
    void setSelected(bool sel) {selected = sel;}
signals:
    void clicked();


protected:

    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *e)
    {
        if(e->buttons() & Qt::RightButton)
        {
            QAction* act = menu->exec(mapToGlobal(e->pos()));
            if(act==open)
            {
                    QProcess* p = new QProcess();
                    p->start(settingsDialog::getInstance()->getDefaultBrowserPath(),QStringList() << data.address);
            }
            else if(act==edit)
            {
                    EditDialog* e = new EditDialog();
                    e->setName(data.name);
                    e->setTags(data.tags);
                    e->setAddress(data.address);

                    connect(e,SIGNAL(accepted()), this, SLOT(editItem()));

                    e->show();
            }
            else if(act==del)
            {
                    ActualDataModel::remove(treeRef);
                    OrganizerWidget::getInstance()->refreshWidget();
            }

        }
        else
        {
            selected = true;
            update();
            emit clicked();
        }
    }

private:
    QAction* open, *edit, *del;
    QMenu* menu;

    QTreeWidgetItem* treeRef;
    QImage image;
    QPixmap pixmap;
    item data;

    QPoint rectOff;
    QPoint tagMarq;
    QTimer* ani_timer;

    QPropertyAnimation* ani, *marq_ani;
    bool selected;

    void renderImage();

    void setRectOff(QPoint& r)
    {
        rectOff = r;
    }
    QPoint& getRectOff()
    {
        return rectOff;
    }

    void resetRectOff()
    {
        rectOff = QPoint(0,75);
    }

    void setTagMarq(QPoint& p)
    {
        tagMarq = p;
    }

    QPoint& getTagMarq()
    {
        return tagMarq;
    }

    void resetTagMarq()
    {
        tagMarq = QPoint(0,0);
    }
public slots:
    void editItem()
    {
        EditDialog* e = (EditDialog*)sender();

        QPixmap p = e->getThumbs();
        QString thumbpath = treeRef->text(THUMB);
        if(!p.isNull())
        {
            thumbpath = e->getName() + ".png";
            p.save(thumbpath);
        }
        ActualDataModel::edit(treeRef,e->getName(),e->getTags(),e->getAddress(),thumbpath);
        OrganizerWidget::getInstance()->refreshWidget();
    }

};
#endif // Thumbnail_H
